package com.wyl.niupi.framework.mybatisPlusExt;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;

/**
 * 格式：where_字段名称_规则： 值
 * @author 吴宇亮 on 2020/6/6 11:26
 */
public class Where {

    public static final String NAME = "where";

    /** 模糊查询字符串, 使用示例：【前端】where_cname_like : xxx, 等价于 NAME like '%xxx%' **/
    public static final String like = "like";
    /** 模糊查询字符串, 使用示例：【前端】where_cname_leftLike : xxx, 等价于 NAME like 'xxx%' **/
    public static final String leftLike = "leftLike";
    /** 模糊查询字符串, 使用示例：【前端】where_cname_rightLike : xxx, 等价于 NAME like '%xxx' **/
    public static final String rightLike = "rightLike";

    /** 精确查询, 使用示例：【前端】where_plid_eq : -1, 等价于 plid = -1 **/
    public static final String eq = "eq";
    /** 精确查询, 使用示例：【前端】where_plid_notEq : -1, 等价于 plid != -1 **/
    public static final String notEq = "notEq";

    /** 比较查询, 使用示例：【前端】where_amount_lessThan : 10, 等价于 amount < 10 **/
    public static final String lessThan = "lessThan";
    /** 比较查询, 使用示例：【前端】where_amount_lessEq : 10, 等价于 amount <= 10 **/
    public static final String lessEq = "lessEq";
    /** 比较查询, 使用示例：【前端】where_amount_bigThan : 10, 等价于 amount > 10 **/
    public static final String bigThan = "bigThan";
    /** 比较查询, 使用示例：【前端】where_amount_bigEq : 10, 等价于 amount >= 10 **/
    public static final String bigEq = "bigEq";

    /** In过滤, 使用示例：【前端】where_lid_in : 1,2,3, 等价于 lid in (1,2,3) **/
    public static final String in = "in";
    /** OR过滤, 使用示例：【前端】where_age_orEq : 1,2,3, 等价于 age=1 or age=2 or age=3); **/
    public static final String orEq = "orEq";
    /** ORLIKE过滤, 正对于字符串, 使用示例：【前端】where_name_orLike : 张三,李四,王五, 等价于 NAME like '%张三%' or age like '%李四%' or age like '%王五%') **/
    public static final String orLike = "orLike";

    /** isNull过滤, 使用示例：【前端】where_age_isNull : '', 等价于 age is null **/
    public static final String isNull = "isNull";
    /** isNotNull过滤, 使用示例：【前端】where_age_isNotNull : '',  等价于 age is not null **/
    public static final String isNotNull = "isNotNull";


    public static <T> void autoFilter(String key, String value, LambdaQueryWrapper<T> queryWrapper) {

    }

    public static <T> void autoFilter(String key, String value, QueryWrapper<T> queryWrapper) {
        String[] arr = key.split("_");
        if(arr.length != 3){
            return; //不符合自动过滤的规则
        }
        String fieldName = arr[1], filterType = arr[2];
        if(StringUtils.isAnyBlank(fieldName, filterType)){
            return;
        }

        if(Where.like.equalsIgnoreCase(filterType)){
            queryWrapper.like(fieldName, value);
        }else if(Where.leftLike.equalsIgnoreCase(filterType)){
            queryWrapper.likeLeft(fieldName, value);
        }else if(Where.rightLike.equalsIgnoreCase(filterType)){
            queryWrapper.likeRight(fieldName, value);
        }else if(Where.eq.equalsIgnoreCase(filterType)){
            queryWrapper.eq(fieldName, value);
        }else if(Where.notEq.equalsIgnoreCase(filterType)){
            queryWrapper.ne(fieldName, value);
        }else if(Where.lessThan.equalsIgnoreCase(filterType)){
            queryWrapper.lt(fieldName, value);
        }else if(Where.lessEq.equalsIgnoreCase(filterType)){
            queryWrapper.le(fieldName, value);
        }else if(Where.bigThan.equalsIgnoreCase(filterType)){
            queryWrapper.gt(fieldName, value);
        }else if(Where.bigEq.equalsIgnoreCase(filterType)){
            queryWrapper.ge(fieldName, value);
        }else if(Where.in.equalsIgnoreCase(filterType)){
            queryWrapper.in(fieldName, value.split(","));
        }else if(Where.orEq.equalsIgnoreCase(filterType)){
            String[] valueArr = value.split(",");
            QueryWrapper<T> and = queryWrapper.and(wrapper -> wrapper.eq(fieldName, valueArr[0]));
            for (int i = 1; i < valueArr.length; i++) {
                String val = valueArr[i];
                and.or().and(wrapper -> wrapper.eq(fieldName, val));
            }
        }else if(Where.orLike.equalsIgnoreCase(filterType)){
            String[] valueArr = value.split(",");
            QueryWrapper<T> and = queryWrapper.and(wrapper -> wrapper.like(fieldName, valueArr[0]));
            for (int i = 1; i < valueArr.length; i++) {
                String val = valueArr[i];
                and.or().and(wrapper -> wrapper.like(fieldName, val));
            }
        }else if(Where.isNull.equalsIgnoreCase(filterType)){
            queryWrapper.isNull(fieldName);
        }else if(Where.isNotNull.equalsIgnoreCase(filterType)){
            queryWrapper.isNotNull(fieldName);
        }

    }
}
